package com.ruoyi.web.controller.kyhs;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.kysh.entity.TeamClassInf;
import com.ruoyi.system.kysh.entity.dto.TeamClassInfReqDto;
import com.ruoyi.system.kysh.entity.dto.TeamClassInfsDto;
import com.ruoyi.system.kysh.service.TeamClassInfService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("班组信息")
@RestController
@RequestMapping("/kyhs/team-class")
public class TeamClassInfController {

    @Autowired
    private TeamClassInfService teamClassInfService;

    @ApiOperation("根据子父部门分页查询 姓名和班组角色选填")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nickName",value = "用户姓名",dataTypeClass = String.class),
            @ApiImplicitParam(name = "postName",value = "职位名",dataTypeClass = String.class),
            @ApiImplicitParam(name = "page",value = "当前页数"),
            @ApiImplicitParam(name = "pageSize",value = "每页容量")
    })
    @GetMapping("/select-by-names")
    public AjaxResult selectByNamesAndPage(@RequestParam Long deptId
            , String nickName, String postName,@RequestParam int page,@RequestParam int pageSize){
        TeamClassInfsDto teamClassInfs = teamClassInfService
                .selectByDeptIdAndNameAndPostAndPage(deptId, nickName, postName, page, pageSize);
        return AjaxResult.success("操作成功",teamClassInfs);
    }

    @ApiOperation("新建班组信息时分页查询未指派班级人员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页数"),
            @ApiImplicitParam(name = "pageSize",value = "每页容量")
    })
    @GetMapping("/show-new-inf")
    public AjaxResult showNewClassInf(Long deptId, int page,int pageSize){
        TeamClassInfsDto teamClassInfs=teamClassInfService.showNewClassInf(deptId,page,pageSize);
        if(teamClassInfs==null||teamClassInfs.getTeamClassInfList().isEmpty()){
            return AjaxResult.warn("人员都已分配至各班");
        }
        return AjaxResult.success("操作成功",teamClassInfs);
    }

    @ApiOperation("将人员指派到所选班组 班组必选")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teamClassInfs",value = "所选人员信息"),
            @ApiImplicitParam(name = "deptName",value = "所指派班组名")
    })
    @PostMapping("/save-by-names")
    public AjaxResult saveClassByName(@RequestBody TeamClassInfReqDto teamClassInfReqDto
            , @RequestParam Long deptId){
//        //是否存在不同区队
//        String parentDeptName =teamClassInfService.isSame(teamClassInfs);
//        if(parentDeptName==null){
//            return AjaxResult.warn("请检查所选人员是否在同一区队下");
//        }

        //更新部门
        teamClassInfService.makeEmpolyInDeptId(teamClassInfReqDto.getTeamClassInfs(), deptId);
        return AjaxResult.success("更新成功");
    }

    @ApiOperation("删除信息")
    @DeleteMapping("/delete")
    public AjaxResult deleteByName(@RequestBody TeamClassInf teamClassInf){
        if(teamClassInf.getParentId().intValue()==0){
            return AjaxResult.warn("无法删除");
        }
        //将部门上移一级
        teamClassInfService.deleteByUserIdAndParentId(teamClassInf.getUserId(),teamClassInf.getParentId());
        return AjaxResult.success("删除成功");
    }

    @ApiOperation("通过区队和班组查询班长名")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptName",value = "班组名",dataTypeClass = String.class),
            @ApiImplicitParam(name = "parentName",value = "区队名",dataTypeClass = String.class),
    })
    @GetMapping("/show-by-dept-name")
    public AjaxResult showUpdateInf(String deptName,String parentName){
        String name=teamClassInfService.findMorByDept(deptName,parentName);
        if(name==null||"".equals(name)){
            return AjaxResult.warn("当前班组未指定班长");
        }
        return AjaxResult.success("操作成功",name);//返回班组长
    }

    @ApiOperation("修改信息")
    @PutMapping("/update-by-name")
    public AjaxResult updateByName(@RequestBody TeamClassInf teamClassInf){
        boolean b=teamClassInfService.morIsExist(teamClassInf);
        if(b){
            return AjaxResult.warn("该班班长已指派");
        }
        String acc = teamClassInfService.findAccByDept(teamClassInf.getDeptName(), teamClassInf.getParentDeptName());
        if(acc!=null){
            return AjaxResult.warn("该班验收员已指派");
        }
        teamClassInfService.updateClassInf(teamClassInf);
        return AjaxResult.success("更新成功");
    }

}
